﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.Data;

namespace MLM.AdminCP.Pages
{
    public partial class ProductSoldStatistics : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StartDate.SelectedDate = DateTime.Now.AddDays(-7);
                EndDate.SelectedDate = DateTime.Now;
            }
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetData(string startDate, string endDate, string productId)
        {
            DateTime _endDate;
            DateTime _startDate;
            if (startDate == "null")
            {
                _startDate = DateTime.Now.AddDays(-7);
            }
            else
            {
                _startDate = DateTime.Parse(startDate);
            }
            if (endDate == "null")
            {
                _endDate = DateTime.Now;
            }
            else
            {
                _endDate = DateTime.Parse(endDate);
            }
            long _productId = 0;
            if (!string.IsNullOrEmpty(productId))
            {
                _productId = long.Parse(productId);
            }
            List<listObject> _listObject = new List<listObject>();

            List<DateTime> listDate = new List<DateTime>();
            List<int> _listValue = new List<int>();
            for (DateTime date = _startDate; date <= _endDate; date = date.AddDays(1))
            {
                listDate.Add(date);
            }
            using (var ct = new MLMDataContext())
            {
                foreach (var item in listDate)
                {
                    DateTime dateStart = new DateTime(item.Year, item.Month, item.Day, 0, 0, 0);
                    DateTime dateEnd = new DateTime(item.Year, item.Month, item.Day, 23, 59, 0);
                    //int _value =
                    //    DataContext.Users.Count(p => p.RegistrationDate >= startdate && p.RegistrationDate <= EndDate);
                    //_listValue.Add(_value);
                    var listOfOrdersByDay =
                        ct.Invoices.Where(
                            it => it.Status == 1 && it.DateTimeCreated >= dateStart && it.DateTimeCreated <= dateEnd);
                    long? value = 0;
                    foreach (var userOrder in listOfOrdersByDay)
                    {
                        foreach (var userOrderDetail in userOrder.InvoiceDetails.Where(it => it.ProductBaseID == _productId))
                        {
                            value += userOrderDetail.Quantity;
                        }
                    }
                    _listValue.Add(int.Parse(value.ToString()));
                }
            }
            _listObject.Add(new listObject { Date = listDate, value = _listValue });
            JavaScriptSerializer ser = new JavaScriptSerializer();
            return ser.Serialize(_listObject);
        }
    }
}